#include "precompiled.h"
#include "item.h"
using namespace weo::engine;
using namespace weo::engine::item;
using namespace std;
Attribute::Attribute()
{	
    m_Type = 0;
    m_Value = 0;
}
Attribute::Attribute(const string type, const float value)
{
    if(AttributeMap.empty())
    {
        InitItem();
    }
    if(type == "DefenseSkillRating")
    {
        m_Type = AttributeMap["DefenseRating"];
    }
    else m_Type = AttributeMap[type];
    m_Value = value;
}
Attribute::Attribute(const unsigned int type, const float value)
{
    if(AttributeMap.empty())
    {
        InitItem();
    }
    m_Type = type;
    m_Value = value;
}
bool Attribute::toXmlElement(ticpp::Element * child) const
{
    if(m_Type == 0) return true;
    try
    {
        child->SetAttribute<std::string>( "type", weo::engine::item::KeyValues[m_Type]);
        child->SetAttribute<std::string>( "value",boost::lexical_cast<string>(m_Value));
    }
    catch( ticpp::Exception& ex )
    {        
        return false;
    }
    return true;
}
bool Attribute::fromXmlElement(const ticpp::Element* child)
{
    try
    {
        if(AttributeMap.empty())
        {
            InitItem();
        }
        if(child != NULL)
        {
            std::string type = child->GetAttribute("type");
            if(!type.empty())
            {
                m_Type = AttributeMap[type];
            }
            m_Value = boost::lexical_cast<float,std::string>(child->GetAttribute("value"));
        }
    }
    catch( ticpp::Exception& ex )
    {        
        return false;
    }
    return true;
}
Sockets::Sockets()
{
    m_Count = 0;
    m_Bonus.clear();
}
Sockets& Sockets::operator=(const Sockets& sockets)
{
    if(this != &sockets)
    {
        m_Count = sockets.Count();
        m_Bonus.clear();
        if(!sockets.Bonus()->empty())
        {
            m_Bonus.assign(sockets.Bonus()->begin(),sockets.Bonus()->end());
        }
        m_Colors.clear();
        if(!sockets.Colors()->empty())
        {
            m_Colors.assign(sockets.Colors()->begin(),sockets.Colors()->end());
        }
    }
    return *this;
}
bool Sockets::toXmlElement(ticpp::Element * root) const
{
    if(m_Count == 0) return true;
    try
    {
        root->SetAttribute<unsigned int>("count",m_Count);
        ticpp::Element * ele = new (UseGC) ticpp::Element("attributes");
        vector<Attribute>::const_iterator it;
        for(it = m_Bonus.begin(); it != m_Bonus.end(); it++)
        {
            ticpp::Element *child = new (UseGC) ticpp::Element("attribute");
            it->toXmlElement(child);
            ele->LinkEndChild( child );
        }
        root->LinkEndChild( ele );
        ele = new (UseGC) ticpp::Element("colors");
        std::vector<unsigned char>::const_iterator cit;
        for(cit = m_Colors.begin(); cit != m_Colors.end(); cit++)
        {
            std::string colorstring = "None";
            switch(*cit)
            {
            case 1:
                colorstring = "Blue";
                break;
            case 2:
                colorstring = "Yellow";
                break;
            case 4:
                colorstring = "Red";
                break;
            case 8:
                colorstring = "Meta";
                break;
            default:
                break;
            }
            ticpp::Element *child = new (UseGC) ticpp::Element("color",colorstring);
            ele->LinkEndChild( child );
        }
        root->LinkEndChild( ele );
    }
    catch( ticpp::Exception& ex )
    {
        
        return false;
    }
    return true;
}
bool Sockets::fromXmlElement(const ticpp::Element* child)
{
    try
    {
        ticpp::Element *pElem = NULL;
        if(child != NULL)
        {
            m_Count = boost::lexical_cast<int,std::string>(child->GetAttribute("count"));
            pElem = child->FirstChildElement("attributes",false);
            if(pElem != NULL)
            {
                ticpp::Element *pAttribute = pElem->FirstChildElement("attribute",false);
                while(pAttribute != NULL)
                {
                    Attribute temp;
                    temp.fromXmlElement(pAttribute);
                    m_Bonus.push_back(temp);
                    pAttribute = pAttribute->NextSiblingElement(false);
                }
            }
            pElem = child->FirstChildElement("colors",false);
            if(pElem != NULL)
            {
                ticpp::Element *pColor = pElem->FirstChildElement("color",false);
                while(pColor != NULL)
                {
                    unsigned char color = GetColor(pColor->GetText(true));
                    m_Colors.push_back(color);
                    pColor = pColor->NextSiblingElement(false);
                }
            }
        }
    }
    catch( ticpp::Exception& ex )
    {
        
        return false;
    }
    return true;
}
Set::Set()
{
    m_Hash = 0;
}
Set& Set::operator=(const Set& set)
{
    if(this != &set)
    {
        m_Name = set.Name();
        m_Hash = set.Hash();
        m_Item.clear();
        if(!set.Item()->empty())
        {
            m_Item.assign(set.Item()->begin(),set.Item()->end());
        }
        m_Bonus.clear();
        if(!set.Bonus()->empty())
        {
            m_Bonus.assign(set.Bonus()->begin(),set.Bonus()->end());
        }
    }
    return *this;
}
bool Set::toXmlElement(ticpp::Element * root) const
{
    if(m_Name.empty()) return true;
    try
    {
        root->SetAttribute("name",m_Name);
        root->SetAttribute<size_t>("hash",m_Hash);
        ticpp::Element * ele = new (UseGC) ticpp::Element("items");
        vector<std::string>::const_iterator cit;
        for(cit = m_Item.begin(); cit != m_Item.end(); cit++)
        {
            ticpp::Element *child = new (UseGC) ticpp::Element("item",*cit);
            ele->LinkEndChild( child );
        }
        root->LinkEndChild( ele ); 
        ele = new (UseGC) ticpp::Element("attributes");
        std::vector<std::pair<unsigned char,Attribute>>::const_iterator it;
        for(it = m_Bonus.begin(); it != m_Bonus.end(); it++)
        {
            ticpp::Element* child = new (UseGC) ticpp::Element("threshold");
            child->SetAttribute("value", boost::lexical_cast<string>((unsigned int)it->first));
            ele->LinkEndChild( child );
            child = new (UseGC) ticpp::Element("attribute");
            it->second.toXmlElement(child);
            ele->LinkEndChild( child );
        }
        root->LinkEndChild( ele );
    }
    catch( ticpp::Exception& ex )
    {
        
        return false;
    }
    return true;
}
bool Set::fromXmlElement(const ticpp::Element* child)
{
    try
    {
        ticpp::Element *pElem = NULL;
        if(child != NULL)
        {
            m_Name = child->GetAttribute("name");
            m_Hash = boost::lexical_cast<size_t, std::string>(child->GetAttributeOrDefault("hash","0"));
            pElem = child->FirstChildElement("items",false);
            if(pElem != NULL)
            {
                ticpp::Element *pItem = pElem->FirstChildElement("item",false);
                while(pItem != NULL)
                {
                    m_Item.push_back(pItem->GetText(true));
                    pItem = pItem->NextSiblingElement(false);
                }
            }
            pElem = child->FirstChildElement("attributes",false);
            if(pElem != NULL)
            {
                ticpp::Element *pThreshold = pElem->FirstChildElement("threshold",false);
                while(pThreshold  != NULL)
                {
                    int threshold = boost::lexical_cast<int,string>(pThreshold->GetAttribute("value"));;
                    ticpp::Element *pAttribute = pThreshold->NextSiblingElement("attribute", true);
                    Attribute temp;
                    temp.fromXmlElement(pAttribute);
                    m_Bonus.push_back(make_pair<int,Attribute>(threshold,temp));
                    pThreshold = pThreshold->NextSiblingElement("threshold", false);
                }
            }
            if(m_Hash == 0)
            {
                boost::hash<std::string> string_hash;
                m_Hash = string_hash(m_Name);
            }
        }
    }
    catch( ticpp::Exception& ex )
    {
        
        return false;
    }
    return true;
}
unsigned char weo::engine::item::Sockets::GetColor(const string& value)
{
    return ColorMap[value];
}
unsigned int weo::engine::item::Attribute::getAttributeValue(const string& value)
{
    return AttributeMap[value];
}
void ::InitItem()
{
    ColorMap["None"] = 0;
    ColorMap["Blue"] = 1;
    ColorMap["blue"] = 1;
    ColorMap["Red"] = 2;
    ColorMap["red"] = 2;
    ColorMap["Yellow"] = 4;
    ColorMap["yellow"] = 4;
    ColorMap["Meta"] = 8;
    ColorMap["meta"] = 8;
    ItemsourceMap["None"] = 1;
    ItemsourceMap["Drop"] = 2;
    ItemsourceMap["Quest"] = 4;
    ItemsourceMap["Recipie"] = 8;
    ItemsourceMap["Vendor"] = 16;
    ItemsourceMap["PVP"] = 32;
    ItemsourceMap["Faction"] = 64;
    ItemsourceMap["Other"] = 128;
    ProfessionMap["Alchemy"] = 1;
    ProfessionMap["Blacksmithing"] = 2;
    ProfessionMap["Enchanting"] = 4;
    ProfessionMap["Engineering"] = 8;
    ProfessionMap["Herbalism"] = 16;
    ProfessionMap["Jewelcrafting"] = 32; 
    ProfessionMap["Leatherworking"] = 64;
    ProfessionMap["Mining"] = 128; 
    ProfessionMap["Skinning"] = 256;
    ProfessionMap["Tailoring"] = 512;
    QualityMap["Junk"] = 1;
    QualityMap["Standard"] = 2;
    QualityMap["Good"] = 4;
    QualityMap["Rare"] = 8;
    QualityMap["Epic"] = 16;
    QualityMap["Legendary"] = 32;
    for(int i = 0;i<KeyValuesLength;i++)
    {
        AttributeMap[KeyValues[i]] = i;
    }
}
Item* Item::s_None = NULL;
Item* Item::None()
{
    if(!s_None)
    {
        s_None = new Item("None",99999);
    }
    return s_None;
}
Item::Item()
{   
    m_Name = "None";
    m_Id = 99999;
    m_Unique = false;
    m_Quality = 0;
    m_Icon = "";
    m_Level = 0;
    m_Type = 0;
    m_Subtype = 0;
    m_Slot = 0;
    m_Speed = -1;
    m_DPS = -1;
    m_Itemsource = 0;
    m_Allowed = 511;
    m_Profession = 0;
}
std::vector<std::string> Item::GetItemSources()
{
    std::vector<std::string> retval;
    std::map<std::string,unsigned int>::const_iterator it;
    for(it = ItemsourceMap.begin(); it != ItemsourceMap.end();it++)
    {
        retval.push_back(it->first);
    }
    return retval;
}
std::vector<std::string> Item::GetProfessions()
{
    std::vector<std::string> retval;
    std::map<std::string,unsigned int>::const_iterator it;
    for(it = ProfessionMap.begin(); it != ProfessionMap.end();it++)
    {
        retval.push_back(it->first);
    }
    return retval;
}
std::vector<std::string> Item::GetQualities()
{
    std::vector<std::string> retval;
    std::map<std::string,unsigned char>::const_iterator it;
    for(it = QualityMap.begin(); it != QualityMap.end();it++)
    {
        retval.push_back(it->first);
    }
    return retval;
}
Item::Item(const std::string &name, const int &id)
{
    *this = Item();
    m_Name = name;
    m_Id = id;
}
Item & Item::operator =(const Item &item)
{
    if(this != &item)
    {
        m_Name = item.Name();
        m_Id = item.Id();
        m_Unique = item.Unique();
        m_Quality = item.Quality();
        m_Icon = item.Icon();
        m_Level = item.Level();
        m_Type = item.Type();
        m_Subtype = item.Subtype();
        m_Slot = item.Slot();
        m_Speed = item.Speed();
        m_DPS = item.DPS();
        m_Allowed = item.Allowed();
        m_Set = *item.Set();
        m_Sockets = *item.Sockets();
        m_Attributes.clear();
        if(!item.Attributes()->empty())
        {
            m_Attributes.assign(item.Attributes()->begin(),item.Attributes()->end());
        }
        m_Areaname.clear();
        if(!item.Areaname()->empty())
        {
            m_Areaname.assign(item.Areaname()->begin(),item.Areaname()->end());
        }
        m_Creaturename.clear();
        if(!item.Creaturename()->empty())
        {
            m_Creaturename.assign(item.Creaturename()->begin(),item.Creaturename()->end());
        }
        m_Itemsource = item.Itemsource();
        m_Profession = item.Profession();
    }
    return *this;
}
bool Item::operator==(const Item& other)
{
    return m_Id == other.Id();
}
bool Item::operator<(const Item& other)
{
    return m_Id < other.Id();
}
void Item::AddAttribute(const weo::engine::item::Attribute &value)
{
    bool found = false;
    bool foundhealing = false; // we don't allow healing and spelldamage on one hit, fucks calculations up
    bool foundspelldamage = false;
    for(unsigned int i = 0;i<m_Attributes.size();i++)
    {
        if((m_Attributes[i].Type() == value.Type()) && value.Type() != weo::engine::item::Attribute::getAttributeValue("Special"))
        {            
            m_Attributes[i].setValue(m_Attributes[i].Value() + value.Value());
            found = true;
            break;
        }
        else if((m_Attributes[i].Type() == value.Type()) && value.Type() == weo::engine::item::Attribute::getAttributeValue("Special"))
        {
            if(m_Attributes[i].Value() == value.Value()) // don't add the same special id twice
            {
                found = true;
                break;
            }
        }
        if(m_Attributes[i].Type() == weo::engine::item::Attribute::getAttributeValue("Healing"))
        {
            foundhealing = true;
        }
        else if(m_Attributes[i].Type() == weo::engine::item::Attribute::getAttributeValue("SpellDamage"))
        {
            foundspelldamage = true;
        }
    }
    if(!found && !(foundhealing && value.Type() == weo::engine::item::Attribute::getAttributeValue("SpellDamage")) && !(foundspelldamage && value.Type() == weo::engine::item::Attribute::getAttributeValue("Healing")))
    {
        m_Attributes.push_back(value);
    }
}
void Item::setAreaname(const std::string value)
{
    bool found = false;
    for(unsigned int i = 0;i<m_Areaname.size();i++)
    {
        if(m_Areaname[i] == value) found = true;
    }
    if(found) return;
    else m_Areaname.push_back(value);
}
void Item::setCreaturename(const std::string value)
{
    bool found = false;
    for(unsigned int i = 0;i<m_Creaturename.size();i++)
    {
        if(m_Creaturename[i] == value) found = true;
    }
    if(found) return;
    else m_Creaturename.push_back(value);
}
void Item::setItemsource(const std::string value)
{
    m_Itemsource = weo::engine::ItemsourceMap[value];
}
unsigned int Item::GetItemsource(const std::string value)
{
    return weo::engine::ItemsourceMap[value];
}
void Item::setProfession(const std::string value)
{
    m_Profession = weo::engine::ProfessionMap[value];
}
unsigned int Item::GetProfession(const std::string value)
{
    return weo::engine::ProfessionMap[value];
}
unsigned char Item::GetQuality(const std::string value)
{
    return weo::engine::QualityMap[value];
}
void Item::setQuality(const std::string value)
{
    m_Quality = weo::engine::QualityMap[value];
}
bool Item::CheckMetaGemRequirements(const weo::engine::item::GemPtr* gems)
{
    // meta gem checks
    int redgems = 0;
    int yellowgems = 0;
    int bluegems = 0;
    int metagemid = 0;
    for(int i = 0;i<weo::engine::Slotcount*3;i++) // bug
    {
        if(gems[i] == NULL) continue;
        if((gems[i]->Subtype() & 1) > 0) bluegems++;
        if((gems[i]->Subtype() & 2) > 0) redgems++;
        if((gems[i]->Subtype() & 4) > 0) yellowgems++;
        if((gems[i]->Subtype() & 8) > 0) metagemid = gems[i]->Id();
        if(i > 2 && metagemid == 0) break;
    }
    bool badmeta = true;
    switch(metagemid)
    {
    case 25890:
    case 25901:
    case 25899:
    case 32409:
    case 32410:
        if(yellowgems >= 2 && bluegems >=2 && redgems >=2)
        {
            badmeta = false;
        }
        break;
    case 32640:
    case 25893:
        if(bluegems > yellowgems)
        {
            badmeta = false;
        }
        break;
    case 28556:
    case 28557:
    case 25894:
        if(yellowgems >= 2 && redgems >=1)
        {
            badmeta = false;
        }
        break;
    case 25897:
        if(yellowgems > bluegems && redgems > bluegems)
        {
            badmeta = false;
        }
        break;
    case 25895:
        if(redgems > yellowgems)
        {
            badmeta = false;
        }
        break;
    case 25896:
        if(bluegems >=3)
        {
            badmeta = false;
        }
        break;
    case 25898:
        if(bluegems >=5)
        {
            badmeta = false;
        }
        break;
    case 34220:
        if(bluegems >=2)
        {
            badmeta = false;
        }
        break;
    case 32641:
        badmeta = false;	
        break;
    default:
        badmeta = false;
        break;
    }
    return !badmeta;
}
/*
    unsigned int m_Id;
    std::string m_Name;
    std::string m_Icon;
    unsigned int m_Level;
    bool m_Unique;
    unsigned char m_Type;
    unsigned char m_Subtype;
    unsigned char m_Slot;
    unsigned char m_Itemsource;
    unsigned char m_Quality;
    float m_Speed;
    float m_DPS;
    unsigned int m_Allowed;
    std::vector<Attribute> m_Attributes;
    Set m_Set;
    Sockets m_Sockets;
    std::vector<std::string> m_Areaname;
    std::vector<std::string> m_Creaturename;
*/
bool Item::toXmlElement(ticpp::Element* root) const
{
    std::string retval = "";
    ticpp::Element* ele;
    ticpp::Element* child;
    try
    {
        root->SetAttribute("id",boost::lexical_cast<unsigned int>(m_Id));
        root->SetAttribute("name",m_Name);
        root->SetAttribute("icon",m_Icon);
        int qualVal = (int) ((std::log((double) m_Quality)/std::log(2.0)) + 0.5);
        root->SetAttribute("quality",qualityRating[qualVal]);
        root->SetAttribute<unsigned int>("level",m_Level);
        if(m_Unique)
        {
            root->SetAttribute("unique","true");
        }
        root->SetAttribute("type",itemType[m_Type]);
        std::string subTypeString = "None";
        std::string slotString = "None";
        if(itemType[m_Type] == "Armor")
        {
            slotString = armorSlot[m_Slot];
            subTypeString = armorType[m_Subtype];
        }
        else if(itemType[m_Type] == "Weapon")
        {
            slotString = weaponSlot[m_Slot];
            subTypeString = weaponType[m_Subtype];
        }
        else if(itemType[m_Type] == "Gem")
        {
            slotString = "None";
            subTypeString = gemType[m_Subtype];
        }
        if(subTypeString != "None")
        {
            root->SetAttribute("subType",subTypeString);
        }
        if(slotString != "None")
        {
            root->SetAttribute("slot",slotString);
        }
        if(m_Itemsource > 0)
        {
            int val = (int) ((std::log((double) m_Itemsource)/std::log(2.0)) + 0.5);
            root->SetAttribute("source",itemSource[val]);
        }
        if(m_Speed > 0)
        {
            root->SetAttribute("speed",boost::lexical_cast<string>(m_Speed));
        }
        if(m_DPS > 0)
        {
            root->SetAttribute("dps",boost::lexical_cast<string>(m_DPS));
        }
        if(m_Allowed != 511)
        {
            root->SetAttribute("allowed",boost::lexical_cast<string>(m_Allowed));
        }
        if(m_Profession > 0)
        {
            int val = (int) ((std::log((double) m_Profession)/std::log(2.0)) + 0.5);
            root->SetAttribute("source",profession[val]);
        }
        ele = new (UseGC) ticpp::Element("attributes");
        vector<Attribute>::const_iterator it;
        for(it = m_Attributes.begin(); it != m_Attributes.end(); it++)
        {
            child = new (UseGC) ticpp::Element("attribute");
            it->toXmlElement(child);
            ele->LinkEndChild( child );
        }
        root->LinkEndChild( ele );
        std::vector<std::string>::const_iterator strit;
        if(!m_Areaname.empty())
        {
            ele = new (UseGC) ticpp::Element("areaName");
            for(strit = m_Areaname.begin();strit != m_Areaname.end(); strit++)
            {
                child = new (UseGC) ticpp::Element("area");
                child->SetAttribute("name",*strit);
                ele->LinkEndChild( child );
            }
            root->LinkEndChild( ele );
        }
        if(!m_Creaturename.empty())
        {
            ele = new (UseGC) ticpp::Element("creatureName");
            for(strit = m_Creaturename.begin();strit != m_Creaturename.end(); strit++)
            {
                child = new (UseGC) ticpp::Element("creature");
                child->SetAttribute("name",*strit);
                ele->LinkEndChild( child );
            }
            root->LinkEndChild( ele );
        }
        if(!m_Set.Item()->empty())
        {
            ele = new (UseGC) ticpp::Element("set");
            m_Set.toXmlElement(ele);
            root->LinkEndChild( ele );
        }
        if(m_Sockets.Count() > 0)
        {
            ele = new (UseGC) ticpp::Element("sockets");
            m_Sockets.toXmlElement(ele);
            root->LinkEndChild( ele );
        }
        
    }
    catch( ticpp::Exception& ex )
    {
        
        std::cout << ex.what() << endl;
        return false;
    }
    return true;
}
/*
    unsigned int m_Id; x
    std::string m_Name; x
    std::string m_Icon; x
    unsigned int m_Level; x
    bool m_Unique; x
    unsigned char m_Type; x 
    unsigned char m_Subtype; x
    unsigned char m_Slot; x
    unsigned char m_Itemsource;
    unsigned char m_Quality; x
    float m_Speed; x
    float m_DPS; x
    unsigned int m_Allowed;
    std::vector<Attribute> m_Attributes;
    Set m_Set;
    Sockets m_Sockets;
    std::vector<std::string> m_Areaname;
    std::vector<std::string> m_Creaturename;
*/
bool Item::fromXmlElement(const ticpp::Element* root)
{
    try
    {
        setId(boost::lexical_cast<unsigned int,std::string>(root->GetAttribute("id")));
        setName(root->GetAttribute("name"));
        std::string icon = root->GetAttributeOrDefault("icon","");
        if(icon.length() > 0)
        {
           setIcon(icon);
        }
        setLevel(boost::lexical_cast<unsigned int, std::string>(root->GetAttributeOrDefault("level","125")));
        string uniqueString = root->GetAttributeOrDefault("unique","");
        setUnique(uniqueString == "true" ? true : false);
        setQuality(Item::GetQuality(root->GetAttribute("quality")));
        setType((unsigned char) matchString(itemType,itemTypeLength, root->GetAttribute("type")));
        setAllowed(boost::lexical_cast<unsigned int, std::string>(root->GetAttributeOrDefault("allowed","511")));
        if(itemType[m_Type] == "Armor")
        {
            setSlot((unsigned char) matchString(armorSlot,armorSlotLength,root->GetAttributeOrDefault("slot","None")));
            setSubtype((unsigned char) matchString(armorType,armorTypeLength,root->GetAttributeOrDefault("subType","None")));
        }
        else if(itemType[m_Type] == "Weapon")
        {
            setSlot((unsigned char) matchString(weaponSlot,weaponSlotLength,root->GetAttributeOrDefault("slot","None")));
            setSubtype((unsigned char) matchString(weaponType,weaponTypeLength,root->GetAttributeOrDefault("subType","None")));
        }
        else if(itemType[m_Type] == "Gem")
        {
            setSlot(0);
            std::string str = root->GetAttributeOrDefault("subType","None");
            typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
            boost::char_separator<char> sep("|");
            tokenizer tokens(str, sep);
            unsigned char subType = 0;
            for (tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end(); tok_iter++)
            {
                unsigned char value = (unsigned char) matchString(gemType,gemTypeLength,*tok_iter);
                value = (unsigned char) pow(2.0,(unsigned char) value-1);
                subType |= value;
            }
            setSubtype(subType);
        }
        string dps = root->GetAttributeOrDefault("dps","");
        string speed = root->GetAttributeOrDefault("speed","");
        if(dps.length() > 0 && speed.length() > 0)
        {
            setSpeed(boost::lexical_cast<float,std::string>(speed));
            setDPS(boost::lexical_cast<float,std::string>(dps));
        }
        string source = root->GetAttributeOrDefault("source","");
        if(source.length() > 0)
        {
            setItemsource(source);
        }
        string profString = root->GetAttributeOrDefault("profession","");
        if(profString.length() > 0)
        {
            setProfession(profString);
        }
        ticpp::Element * pElem = root->FirstChildElement("attributes",true);
        if(pElem != NULL)
        {
            ticpp::Element *pAttr = pElem->FirstChildElement("attribute",false);
            while(pAttr != NULL)
            {
                Attribute attr;
                attr.fromXmlElement(pAttr);
                AddAttribute(attr);
                pAttr = pAttr->NextSiblingElement(false);
            }
        }
        pElem = root->FirstChildElement("areaName",false);
        if(pElem != NULL)
        {
            ticpp::Element *child = pElem->FirstChildElement("area");
            while(child != NULL)
            {
                setAreaname(child->GetAttribute("name"));
                child = child->NextSiblingElement(false);
            }
        }
        pElem = root->FirstChildElement("creatureName",false);
        if(pElem != NULL)
        {
            ticpp::Element *child = pElem->FirstChildElement("creature");
            while(child != NULL)
            {
                setCreaturename(child->GetAttribute("name"));
                child = child->NextSiblingElement(false);
            }
        }
        pElem = root->FirstChildElement("set",false);
        if(pElem != NULL)
        {
            weo::engine::item::Set set;
            set.fromXmlElement(pElem);
            setSet(set);
        }
        pElem = root->FirstChildElement("sockets",false);
        if(pElem != NULL)
        {
            weo::engine::item::Sockets socket;
            socket.fromXmlElement(pElem);
            setSockets(socket);
        }
    }
    catch( ticpp::Exception& ex )
    {
        setId(99999);        
        return false;
    }
    return true;
}